package xiaozhi.modules.sys;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class InitAdminUserWithJdbc {

    public static void main(String[] args) {
        // 数据库连接信息
        String url = "jdbc:mysql://localhost:3306/xiaozhi_esp32";
        String username = "root";
        String password = "";

        // SQL脚本路径
        String sqlScriptPath = "f:\\Aixiaozhi\\xiaozhi-esp32-server\\init_admin_user.sql";

        try (Connection conn = DriverManager.getConnection(url, username, password);
             Statement stmt = conn.createStatement();
             BufferedReader reader = new BufferedReader(new FileReader(sqlScriptPath))) {

            // 读取并执行SQL脚本
            String line;
            StringBuilder sqlBuilder = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                // 跳过注释行和空行
                if (line.startsWith("--") || line.trim().isEmpty()) {
                    continue;
                }

                sqlBuilder.append(line);

                // 如果遇到分号，执行SQL语句
                if (line.endsWith(";")) {
                    String sql = sqlBuilder.toString();
                    stmt.executeUpdate(sql);
                    sqlBuilder.setLength(0);
                }
            }

            System.out.println("管理员用户初始化成功");

        } catch (SQLException | IOException e) {
            System.err.println("管理员用户初始化失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}